package com.zrf.gulimall.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrf.gulimall.common.database.utils.PageUtils;
import com.zrf.gulimall.common.database.utils.Query;
import com.zrf.gulimall.product.dao.SkuInfoDao;
import com.zrf.gulimall.product.entity.SkuImagesEntity;
import com.zrf.gulimall.product.entity.SkuInfoEntity;
import com.zrf.gulimall.product.entity.SpuInfoDescEntity;
import com.zrf.gulimall.product.service.*;
import com.zrf.gulimall.product.vo.web.SkuItemSaleAttrVo;
import com.zrf.gulimall.product.vo.web.SkuItemVo;
import com.zrf.gulimall.product.vo.web.SpuItemAttrGroupVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
	@Resource
	private ThreadPoolExecutor executor;

	@Resource
	private SkuImagesService skuImagesService;

	@Resource
	private SpuInfoDescService spuInfoDescService;

	@Resource
	private AttrGroupService attrGroupService;

	@Resource
	private SkuSaleAttrValueService skuSaleAttrValueService;

	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
		String key = (String) params.get("key");
		String catelogId = (String) params.get("catelogId");
		String brandId = (String) params.get("brandId");
		String min = (String) params.get("min");
		String max = (String) params.get("max");
		IPage<SkuInfoEntity> page = this.lambdaQuery()
				.eq(StrUtil.isNotEmpty(brandId) && !"0".equals(brandId), SkuInfoEntity::getBrandId, brandId)
				.eq(StrUtil.isNotEmpty(catelogId) && !"0".equals(catelogId), SkuInfoEntity::getCatalogId, catelogId)
				.ge(StrUtil.isNotEmpty(min), SkuInfoEntity::getPrice, min)
				.le(StrUtil.isNotEmpty(max) && !"0".equals(max), SkuInfoEntity::getPrice, max)
				.and(StrUtil.isNotEmpty(key), wrapper -> wrapper.eq(SkuInfoEntity::getSkuId, key).or().like(SkuInfoEntity::getSkuName, key))
				.page(new Query<SkuInfoEntity>().getPage(params));
		return new PageUtils(page);
	}

	@Override
	public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {
		return this.lambdaQuery()
				.eq(SkuInfoEntity::getSpuId, spuId)
				.list();
	}

	@Override
	public SkuItemVo itemInfo(String skuId) throws ExecutionException, InterruptedException {
		SkuItemVo skuItemVo = new SkuItemVo();

		CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
			//1、sku基本信息的获取  pms_sku_info
			SkuInfoEntity info = this.getById(skuId);
			skuItemVo.setInfo(info);
			return info;
		}, executor);


		CompletableFuture<Void> saleAttrFuture = infoFuture.thenAcceptAsync((res) -> {
			//3、获取spu的销售属性组合
			if (Objects.nonNull(res)) {
				List<SkuItemSaleAttrVo> skuItemSaleAttrVos = skuSaleAttrValueService.getSaleAttrBySpuId(res.getSpuId());
				skuItemVo.setSaleAttr(skuItemSaleAttrVos);
			}
		}, executor);


		CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
			//4、获取spu的介绍    pms_spu_info_desc
			if (Objects.nonNull(res)) {
				SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
				skuItemVo.setDesc(spuInfoDescEntity);
			}
		}, executor);


		CompletableFuture<Void> baseAttrFuture = infoFuture.thenAcceptAsync((res) -> {
			//5、获取spu的规格参数信息
			if (Objects.nonNull(res)) {
				List<SpuItemAttrGroupVo> attrGroupVos = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
				skuItemVo.setGroupAttrs(attrGroupVos);
			}
		}, executor);

		//2、sku的图片信息    pms_sku_images
		CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
			List<SkuImagesEntity> imagesEntities = skuImagesService.getImagesBySkuId(skuId);
			skuItemVo.setImages(imagesEntities);
		}, executor);
//
//		CompletableFuture<Void> seckillFuture = CompletableFuture.runAsync(() -> {
//			//3、远程调用查询当前sku是否参与秒杀优惠活动
//			R skuSeckilInfo = seckillFeignService.getSkuSeckilInfo(skuId);
//			if (skuSeckilInfo.getCode() == 0) {
//				//查询成功
//				SeckillSkuVo seckilInfoData = skuSeckilInfo.getData("data", new TypeReference<SeckillSkuVo>() {
//				});
//				skuItemVo.setSeckillSkuVo(seckilInfoData);
//
//				if (seckilInfoData != null) {
//					long currentTime = System.currentTimeMillis();
//					if (currentTime > seckilInfoData.getEndTime()) {
//						skuItemVo.setSeckillSkuVo(null);
//					}
//				}
//			}
//		}, executor);


		//等到所有任务都完成
		CompletableFuture.allOf(saleAttrFuture, descFuture, baseAttrFuture, imageFuture).get();

		return skuItemVo;
	}

}